package com.zc.im.service.im.group.impl;

import com.zc.im.mapper.im.group.ZcImGroupMarkScoreMapper;
import com.zc.im.mapper.im.group.ZcImGroupMemberMapper;
import com.zc.im.model.im.group.ZcImGroupMarkScore;
import com.zc.im.model.im.group.ZcImGroupMarkScoreExample;
import com.zc.im.model.im.group.ZcImGroupMarkScoreKey;
import com.zc.im.service.im.group.ImGroupMarkScoreService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 打分实现
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupMarkScoreServiceImpl implements ImGroupMarkScoreService {


    @Autowired
    private ZcImGroupMarkScoreMapper groupMarkScoreMapper;
    private static Logger logger = Logger.getLogger(ImGroupMarkScoreServiceImpl.class);


    /**
     * 添加打分
     * @param zcImGroupMarkScoreModel
     * @return
     */
    @Override
    public Integer insertGroupMarkScore(ZcImGroupMarkScore zcImGroupMarkScoreModel) {
        Integer groupMarkScoreId = null;
        if (zcImGroupMarkScoreModel == null) {
            logger.info("insert groupMarkScore is  null");
            return groupMarkScoreId;
        }
        try {
            zcImGroupMarkScoreModel.setCreatedTime(new Date());
            zcImGroupMarkScoreModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
            groupMarkScoreId = groupMarkScoreMapper.insertSelective(zcImGroupMarkScoreModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupMarkScoreId;
    }


    /**
     * 删除打分
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupMarkScore(Integer id) {
        Integer groupMarkScoreId = null;
        if (id == null) {
            logger.info("delete GroupMarkScore is null");
            return groupMarkScoreId;
        }
        try {
            ZcImGroupMarkScoreKey key = new ZcImGroupMarkScoreKey();
            key.setId(id);
            groupMarkScoreId = groupMarkScoreMapper.deleteByPrimaryKey(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupMarkScoreId;


    }

    /**
     * 更新打分
     * @param zcImGroupMarkScore
     * @return
     */
    @Override
    public Integer updateGroupMarkScore(ZcImGroupMarkScore zcImGroupMarkScore) {
        Integer groupMarkScoreId = null;
        if (zcImGroupMarkScore == null) {
            logger.info("update  groupMarkScore is  null");
            return groupMarkScoreId;
        }
        try {
            zcImGroupMarkScore.setUpdatedTime(new Date());
            ZcImGroupMarkScoreExample scoreExample = new ZcImGroupMarkScoreExample();
            ZcImGroupMarkScoreExample.Criteria criteria = scoreExample.createCriteria();
            criteria.andIdEqualTo(zcImGroupMarkScore.getId());

            groupMarkScoreId = groupMarkScoreMapper.updateByExampleSelective(zcImGroupMarkScore, scoreExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupMarkScoreId;
    }

    /**
     * 通过日志id  查询分数
     * @param zcImGroupMarkScoreModel
     * @return
     */
    @Override
    public List<ZcImGroupMarkScore> queryAllGroupMarkScore(ZcImGroupMarkScore zcImGroupMarkScoreModel) {
        List<ZcImGroupMarkScore> zcImGroupMarkScoreModels = new ArrayList<>();
        if (zcImGroupMarkScoreModel == null) {
            logger.info("queryAll GroupMarkScore is null");
            return zcImGroupMarkScoreModels;
        }
        try {
            ZcImGroupMarkScoreExample scoreExample = new ZcImGroupMarkScoreExample();
            scoreExample.createCriteria().andLogIdEqualTo(zcImGroupMarkScoreModel.getLogId());

            zcImGroupMarkScoreModels = groupMarkScoreMapper.selectByExample(scoreExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupMarkScoreModels;
    }


    /**
     * 添加评分
     * @param zcImGroupMarkScoreModel
     * @return
     */
    @Override
    public Integer saveGroupMarkScore(ZcImGroupMarkScore zcImGroupMarkScoreModel) {
        Integer groupMarkScoreKey = null;
        if (zcImGroupMarkScoreModel == null) {
            return groupMarkScoreKey;
        }
        try {
            List<ZcImGroupMarkScore> zcImGroupMarkScore =  queryAllGroupMarkScore(zcImGroupMarkScoreModel);
            if (zcImGroupMarkScore==null || zcImGroupMarkScore.size()==0) {
                groupMarkScoreKey = insertGroupMarkScore(zcImGroupMarkScoreModel);
            } else {
                logger.error("已经打过分数了不能重复打分");
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupMarkScoreKey;
    }


    @Override
    public ZcImGroupMarkScore queryGroupMarkScore(Integer zcImGroupMarkScoreModel) {
        ZcImGroupMarkScore zcImGroupMarkScore = null;
        if (zcImGroupMarkScoreModel == null) {
            return zcImGroupMarkScore;
        }
        ZcImGroupMarkScore zcImGroupMarkScore1 = new ZcImGroupMarkScore();
        zcImGroupMarkScore1.setLogId(zcImGroupMarkScoreModel);
        try {
            List<ZcImGroupMarkScore> listZcImGroupMarkScore = queryAllGroupMarkScore(zcImGroupMarkScore1);
            if (listZcImGroupMarkScore == null || listZcImGroupMarkScore.size() == 0) {
                return zcImGroupMarkScore;
            }
            zcImGroupMarkScore = listZcImGroupMarkScore.get(0);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupMarkScore;
    }


}



